package me.zhengjie.hr.positionbase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.hr.positionbase.domain.PositionBase;
import me.zhengjie.hr.positionbase.service.PositionBaseService;
import me.zhengjie.hr.positionbase.service.dto.PositionBaseQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Api(tags = "职位基础信息管理")
@RestController
@RequestMapping("/api/positionBase")
public class PositionBaseController {

    private final PositionBaseService positionBaseService;

    public PositionBaseController(PositionBaseService positionBaseService) {
        this.positionBaseService = positionBaseService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('positionBase:list')")
    public void download(HttpServletResponse response, PositionBaseQueryCriteria criteria) throws IOException {
        positionBaseService.download(positionBaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询职位基础信息")
    @ApiOperation("查询职位基础信息")
    @PreAuthorize("@el.check('positionBase:list')")
    public ResponseEntity<Object> getPositionBases(PositionBaseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(positionBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增职位基础信息")
    @ApiOperation("新增职位基础信息")
    @PreAuthorize("@el.check('positionBase:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody PositionBase resources){
        return new ResponseEntity<>(positionBaseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改职位基础信息")
    @ApiOperation("修改职位基础信息")
    @PreAuthorize("@el.check('positionBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody PositionBase resources){
        positionBaseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除职位基础信息")
    @ApiOperation("删除职位基础信息")
    @PreAuthorize("@el.check('positionBase:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        positionBaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}